import request from "@/axios/request";

const {ElMessage} = require("element-plus");

export default {
    name: "LandlordHome",
    data() {
        return {
            loading: true,
            landlordId: null,
            username: "",
            password: "",
            phone: "",
            email: "",
            // landlordId: 1,
            // username: "FelicityZ",
            // password: "123456",
            // phone: "19853228572",
            // email: "1833808950@qq.com",
            images: [
                { src: require('@/assets/landlord_banner1.png'), alt: 'pic1' },
                { src: require('@/assets/landlord_banner2.png'), alt: 'pic2' },
                { src: require('@/assets/landlord_banner4.png'), alt: 'pic3' },
                { src: require('@/assets/landlord_banner3.png'), alt: 'pic4' }
            ],
            total: 0,
            allData: [], 
            // firstSixData: [],
            firstSixData: [{
                houseId: 1,
                title: "学院十一峯",
                description: "学院十一峯位于温州市鹿城区滨江街道会展路与学院路交汇处，由金地集团与新希望地产联合开发，2021年竣工交付。项目总户数1276户，包含11栋板塔结合建筑，绿化率30%，容积率3.13，物业费4.7元/㎡·月（含能耗费）。小区定位改善型住宅，主力户型为143-189㎡的四居室，南北通透设计，精装修交付。‌‌",
                price: "5000",
                address: "温州市鹿城区滨江街道会展路与学院路交汇处",
                area: "浙江省",
                size: 140,
                rooms: 3,
                floor: 3,
                landlordId: 1,
                status: "待审核",
                imageUrl: "https://img2.baidu.com/it/u=668963934,1194070255&fm=253&fmt=auto&app=138&f=JPEG?w=661&h=500"
            },{
                houseId: 1,
                title: "学院十一峯",
                description: "学院十一峯位于温州市鹿城区滨江街道会展路与学院路交汇处，由金地集团与新希望地产联合开发，2021年竣工交付。项目总户数1276户，包含11栋板塔结合建筑，绿化率30%，容积率3.13，物业费4.7元/㎡·月（含能耗费）。小区定位改善型住宅，主力户型为143-189㎡的四居室，南北通透设计，精装修交付。‌‌",
                price: "5000",
                address: "温州市鹿城区滨江街道会展路与学院路交汇处",
                area: "浙江省",
                size: 140,
                rooms: 3,
                floor: 3,
                landlordId: 1,
                status: "待审核",
                imageUrl: "https://img2.baidu.com/it/u=668963934,1194070255&fm=253&fmt=auto&app=138&f=JPEG?w=661&h=500"
            }],
            statusMap: {
                rented: { text: '已租', type: 'info' },
                available: { text: '待租', type: 'success' },
                pending: { text: '审核中', type: 'warning' },
                rejected: { text: '审核未通过', type: 'danger' }
            }
        };
    },
    computed: {
        paginatedData() {
            const start = 0;
            const end = 6;
            return this.allData.slice(start, end);
        }
    },
    created() {
        this.loadMyInfo();
        this.load();
        this.loading = true;
        setTimeout(() => {
            this.loading = false;
        }, 1000);
    },
    methods: {
        async load() {
            try{
                const res = await request.get("/house/findAll");
                if (res.code === 200) {
                    this.allData = res.data || [];
                    this.total = this.allData.length;
                    this.firstSixData = this.paginatedData;
                } else {
                    ElMessage.error(res.msg || "加载失败");
                    this.allData = [];
                    this.firstSixData = [];
                    this.total = 0;
                }
                this.loading = false;
            } catch (err){
                console.error(err);
                ElMessage.error("网络错误，请稍后再试");
                this.allData = [];
                // this.firstSixData = [];
                this.total = 2; 
                this.loading = false;
            }
        },
        loadMyInfo() {
            const token = JSON.parse(window.sessionStorage.getItem('userInfo'));

            if(!token) {
                ElMessage.error("获取用户信息失败");
                console.log("获取用户信息失败");
                return;
            }

            this.landlordId = token.landlordId;
            this.username = token.username;
            this.password = token.password;
            this.phone = token.phone;
            this.email = token.email;
        },
        SignOut() {
            sessionStorage.clear()
            ElMessage({
                message: '用户退出登录',
                type: 'success',
            });
            this.$router.replace({path: '/Login'});
        },
        // 状态标签类型获取方法
        getStatusTagType(status) {
            return this.statusMap[status]?.type || '';
        },
        // 状态标签文本获取方法
        getStatusLabel(status) {
            return this.statusMap[status]?.text || '未知状态';
        }
    },
};